package com.frxuexi.park2212.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.SearchView.OnQueryTextListener
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding,ParkViewModel>() {
    val parkPageAdapter by lazy { ParkPageAdapter() }
    var para="-1"//输入参数
    var type=""
    override fun initData() {

    }

    override fun initView() {
        binding.headPark.headerTitle.setText("车位管理")
        binding.headPark.headerAdd.setOnClickListener {
            ARouter.getInstance().build("/app/ParkAddActivity").navigation()
        }
        binding.rvPark.apply {
            adapter=parkPageAdapter
            layoutManager=LinearLayoutManager(this@ParkActivity)
        }

        parkPageAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(position: Int, item: ParkBeanItem) {
                ARouter.getInstance().build("/app/ParkEditActivity").withSerializable("bean",item).navigation()
//                parkPageAdapter.notifyItemRemoved(position)
            }

        })

        parkPageAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(item: ParkBeanItem) {
                ARouter.getInstance().build("/app/ParkVRActivity").navigation()
            }

        })

        val helper =
            ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
                override fun onMove(
                    recyclerView: RecyclerView,
                    viewHolder: RecyclerView.ViewHolder,
                    target: RecyclerView.ViewHolder
                ): Boolean {
                    return false
                }

                override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                    val position = viewHolder.adapterPosition
                    parkPageAdapter.notifyItemRemoved(position)

                }

            })
        helper.attachToRecyclerView(binding.rvPark)



        binding.parkFilter.setOnClickListener {
            binding.drawer.openDrawer(GravityCompat.END)
        }

        binding.drawRg.setOnCheckedChangeListener { group, checkedId ->
            binding.drawer.closeDrawer(GravityCompat.END)
            var status=""
            if(checkedId==R.id.rb_kx){
                status="空闲"
            }else if(checkedId==R.id.rb_zy){
                status="已占用"
            }else{
                status="维护中"
            }
            lifecycleScope.launch {
                vm.getParksByType(status,"状态").collect{
                    parkPageAdapter.submitData(it)
                }
            }

        }

        binding.parkSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                para=query!!
                if(type.isNotEmpty()){
                    lifecycleScope.launch {
                        vm.getParksByType(para,type).collect{
                            parkPageAdapter.submitData(it)
                        }
                    }
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }

        })

        binding.drawArea.setOnClickListener {
            type="区域"
            binding.drawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                if(para=="-1"){
                    ToastUtils.showLong("请输入区域ID")
                    return@launch
                }
                vm.getParksByType(para,type).collect{
                    parkPageAdapter.submitData(it)
                }
            }
        }

        binding.drawType.setOnClickListener {
            type="类型"
            binding.drawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                if(para=="-1"){
                    ToastUtils.showLong("请输入类型")
                    return@launch
                }
                vm.getParksByType(para,type).collect{
                    parkPageAdapter.submitData(it)

                }
            }
        }

        

    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            vm.getParksByType("标准车位","类型").collect{
                parkPageAdapter.submitData(it)

            }
        }
    }

}